//
//  TotailsViewController.m
//  HMEMSApp
//
//  Created by chirui on 2018/6/26.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "TotailsViewController.h"
#import "PopuDetailsTableViewCell.h"

@interface TotailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSMutableArray * _dataArray;
    int _totailPage;
    int _page;
    UITableView * _tableView;
    NSDictionary * userDic;
    
    //时间选择
    UIView * dataPickBgView ; // 背景颜色
    UIDatePicker * datePicker;
    NSString * timeType;
    
    UIButton * starTime;
    UIButton * endTime;
    
    NSString * starTimeStr;
    NSString * endTimeStr;
}

@end

@implementation TotailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _dataArray = @[].mutableCopy;
    
    TitleView(@"单量统计");
    [self creatTableView];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    
    starTimeStr = str;
    endTimeStr =  str;
    
    [self getMessageListData];
    
    dataPickBgView =[[UIView alloc]initWithFrame:CGRectMake(0,  HEIGHT -200-94, WIDTH, 230)];
    dataPickBgView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    dataPickBgView.hidden=YES;
    datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(0,30, WIDTH, 200)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.backgroundColor =[UIColor groupTableViewBackgroundColor];
    [datePicker addTarget:self action:@selector(chooseDate:) forControlEvents:UIControlEventValueChanged];
    NSDate * date =[[NSDate alloc]init];
    datePicker.maximumDate = date;
    
    [dataPickBgView addSubview:datePicker];
    
    UIButton * cannelBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    cannelBtn.frame = CGRectMake(0, 0, 80, 30);
    [cannelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cannelBtn addTarget:self action:@selector(cannelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [dataPickBgView addSubview:cannelBtn];
    
    UIButton * sureBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    sureBtn.frame = CGRectMake(WIDTH - 80, 0, 80, 30);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(chooseTimeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [dataPickBgView addSubview:sureBtn];
    
    
    [self.view addSubview:dataPickBgView];
    
}

-(void)cannelBtnClick:(UIButton *)btn {
    
    dataPickBgView.hidden =YES;
}

-(void)chooseTimeBtnClick:(UIButton *)btn {
    
    dataPickBgView.hidden =YES;
    _page =1;
    [self getMessageListData];
}

-(void)creatTableView {
    
    _tableView  =[[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT-SH_TopHeight)
                                             style:UITableViewStyleGrouped];
    _tableView.dataSource=self;
    _tableView.delegate =self;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView =view;
    
    [_tableView registerNib:[UINib nibWithNibName:@"PopuDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:[PopuDetailsTableViewCell reuseIdentifier]];
    
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat{
    
    return 60;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return  170;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary * dic = _dataArray[indexPath.row];
    PopuDetailsTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[PopuDetailsTableViewCell reuseIdentifier]];
    
    cell.timeLabel.text = dic[@"pay_time"];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@",dic[@"order_sn"]];
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    NSDictionary * dic = _dataArray[indexPath.row];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 170)];
    topView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    
   
    
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 70, WIDTH-20, 30)];
    titleLabel.text =[NSString stringWithFormat:@"当前筛选单量 %@",userDic[@"total_count"]];
//    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:titleLabel];

    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 140, WIDTH, 1)];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [topView addSubview:lineView];
    
    UILabel * starLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 60, 49)];
    starLabel.text = @"开始时间";
    starLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:starLabel];
    
    starTime = [UIButton buttonWithType:UIButtonTypeCustom];
    starTime.frame = CGRectMake(70, 20, WIDTH/2-80, 49);
    [starTime setTitle:starTimeStr forState:UIControlStateNormal];
    [starTime setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [starTime addTarget:self action:@selector(statTimeClick:) forControlEvents:UIControlEventTouchUpInside];
    starTime.titleLabel.font = [UIFont systemFontOfSize:15];
//    starTime.imageEdgeInsets = UIEdgeInsetsMake(20,WIDTH/2-40,20,20);
//    [starTime setImage:ImageName(@"icon_class") forState:UIControlStateNormal];
    [topView addSubview:starTime];
    
    UILabel * endLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2, 20, 60, 49)];
    endLabel.text = @"结束时间";
    endLabel.font = [UIFont systemFontOfSize:14];
    [topView addSubview:endLabel];
    
    endTime = [UIButton buttonWithType:UIButtonTypeCustom];
    endTime.frame = CGRectMake(WIDTH/2+70, 20, WIDTH/2-80, 49);
    [endTime setTitle:endTimeStr forState:UIControlStateNormal];
    endTime.titleLabel.font = [UIFont systemFontOfSize:15];
    [endTime setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [endTime addTarget:self action:@selector(endTimeClick:) forControlEvents:UIControlEventTouchUpInside];
//    endTime.imageEdgeInsets = UIEdgeInsetsMake(20,WIDTH/2-40,20,20);
//    [endTime setImage:ImageName(@"icon_class") forState:UIControlStateNormal];
    [topView addSubview:endTime];
    
    UIView * titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 120, WIDTH, 50)];
    titleView.backgroundColor = [UIColor whiteColor];
    [topView addSubview:titleView];
    
    UILabel * orderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 80, 30)];
    orderLabel.text = @"订单时间";
    orderLabel.textAlignment = NSTextAlignmentCenter;
    orderLabel.textColor = [UIColor lightGrayColor];
    orderLabel.font = [UIFont systemFontOfSize:14];
    [titleView addSubview:orderLabel];
    
    UILabel *snLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH -90, 15, 80, 30)];
    snLabel.text = @"订单编号";
    snLabel.textAlignment = NSTextAlignmentCenter;
    snLabel.textColor = [UIColor lightGrayColor];
    snLabel.font = [UIFont systemFontOfSize:14];
    [titleView addSubview:snLabel];
    
    return topView;
}

-(void)statTimeClick:(UIButton *)btn{
    timeType = @"stat";
    dataPickBgView.hidden = NO;
}

-(void)endTimeClick:(UIButton *)btn{
    timeType = @"end";
    dataPickBgView.hidden = NO;
}

-(void)chooseDate:(UIDatePicker *)picker{
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    if ([timeType isEqualToString:@"stat"]) {
        starTimeStr = [formatter stringFromDate:picker.date] ;
        [starTime setTitle:[NSString stringWithFormat:@"%@",starTimeStr] forState:UIControlStateNormal];
    }
    if ([timeType isEqualToString:@"end"]) {
        endTimeStr = [formatter stringFromDate:picker.date] ;
        if ([SHJudgeTools compareDate:endTimeStr withDate:starTimeStr] == 1) {
            SHuiMBhudTextTimeStr(@"截止日期不能小于开始日期");
            endTimeStr = [formatter stringFromDate:[NSDate date]];
        }else {
            [endTime setTitle:[NSString stringWithFormat:@"%@",[formatter stringFromDate:picker.date]] forState:UIControlStateNormal];
        }
    }
}

-(void)getMessageListData{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];

            return ;
        }
    }
    NSDictionary * dic = @{@"token":USER_DEFAULTGet(@"token"),
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"page_num":@"10",
                           @"start_time":starTimeStr,
                           @"end_time":endTimeStr
                           };

    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(order_totails_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"datas"][@"total_page"] intValue];
            userDic = responseObject[@"datas"];
            if (_page==1) {

                if (_dataArray) {

                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {

                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {

                        [_dataArray addObject:dic];
                    }
                }
            }else {

                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {

                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {

                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{

                if (_dataArray.count>0) {

                    if (_dataArray.count<10) {

                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;

                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
            });
        }else {

            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }

    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
}

@end
